package com.jiantong.web.open.app.work;

import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.DiagnosisReport;
import com.jiantong.work.service.DiagnosisReportReadService;
import com.jiantong.work.service.DiagnosisReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

/**
 *@author 毛文杰
 * -- 防疫中心-诊疗记录
 */
@Slf4j
@OpenBean
public class DiagnosisReportApis {
    @RpcConsumer
    private DiagnosisReportReadService diagnosisReportReadService;
    @RpcConsumer
    private DiagnosisReportWriteService diagnosisReportWriteService;
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;

    @OpenMethod(key = "find.diagnosis.report.by.id", paramNames = {"id"}, httpMethods = RequestMethod.GET)
    public DiagnosisReport findById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<DiagnosisReport> resp = diagnosisReportReadService.findById(id);
        if (!resp.isSuccess()){
            log.error("failed to diagnosis report by id = {}, cause : {}", id, resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }


    @OpenMethod(key = "create.diagnosis.report",
            paramNames = {"diagnosisReport","toStatus"},
            httpMethods = RequestMethod.POST)
    public Map<String,Object> create(DiagnosisReport diagnosisReport,Integer toStatus){
        Response<Map<String,Object>>response = diagnosisReportWriteService.create(diagnosisReport,toStatus);
        if(!response.isSuccess()){
            log.error("failed to create diagnosis report  by diagnosisReport = {}, cause : {}", diagnosisReport, response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }
    @OpenMethod(
            key = "paging.diagnosis.report",
            paramNames = {"pageNo", "pageSize","status","breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<DiagnosisReport> pagingDiagnosisReport(Integer pageNo, Integer pageSize, Integer status ,Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }
        Response <Paging<DiagnosisReport>> resp = diagnosisReportReadService.pagingDiagnosisReport(pageNo,pageSize,status,breedFactoryId);
        if(!resp.isSuccess()){
            log.error("failed to paging diagnosis report by id = {}, cause : {}", status, resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

    @OpenMethod(
            key = "update.diagnosis.report",
            paramNames = {"diagnosisReport","toStatus"},
            httpMethods=RequestMethod.PUT
    )
    public Map<String , Object> update(DiagnosisReport diagnosisReport,Integer toStatus){
        if(diagnosisReport.getId() == null){
            log.error("id should be not null error = {}, cause : {}", diagnosisReport,diagnosisReport.getId());
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<Map<String , Object>> statusResponse = diagnosisReportReadService.findStatusById(diagnosisReport.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(diagnosisReport.getStatus())){
            log.error("diagnosisReport is already submitted error = {}, cause : {}", diagnosisReport.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException(ExceptionCode.DOCUMENT_REPEAT_SUBMIT.value(),
                    "this.report.is.already.submitted");
        }
        Response<Map<String , Object>>response = diagnosisReportWriteService.update(diagnosisReport,toStatus);
        if (!response.isSuccess()){
            log.error("failed to update  diagnosis report by diagnosisReport = {}, cause : {}", diagnosisReport, response.getError());
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }

    /**
     * 政府端时间查询
     *
     * @return status =1
     */
    @OpenMethod(
            key = "paging.status.diagnosis.report",
            paramNames = {"pageNo", "pageSize", "breedFactoryId","breedType", "fromDate", "toDate"},
            httpMethods = RequestMethod.GET)
    public Paging<DiagnosisReport> pagingDiagnosisReport(Integer pageNo,
                                                         Integer pageSize,
                                                         Long breedFactoryId,
                                                         Integer breedType,
                                                         String fromDate,
                                                         String toDate) {

        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<Paging<DiagnosisReport>> resp =
                diagnosisReportReadService.pagingDiagnosisReportGqv(pageNo, pageSize, breedFactoryId,breedType, start, end);
        if (!resp.isSuccess()) {
            log.error("failed to paging diagnosisReport center{}, cause : {}", resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

}
